package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.constant.TableEnum;
import com.hbx.logs.annotation.MarkLog;
import com.hbx.logs.OperaTypeEnum;
import com.hbx.dto.notice.NoticeInsertUpdateDto;
import com.hbx.exception.RspException;
import com.hbx.service.NoticeService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-22
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "邮件反馈处理器")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping(value = "/getNoticeList",method = RequestMethod.GET)
    @ApiOperation("获取反馈人信息")
    public Result getNoticeList(@RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize){
        if (pageNum == null || pageNum < 0){
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR,"pageNum传递有误");
        }
        if (pageSize == null || pageSize < 0){
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR,"pageSize传递有误");
        }
        return noticeService.getNoticeList(pageNum,pageSize);
    }

    /**
     * 删除反馈人信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/removeNotice",method = RequestMethod.GET)
    @ApiOperation("删除反馈人信息")
    @MarkLog(operate = OperaTypeEnum.DELETE,tables = TableEnum.NOTICE, desc = "管理员删除反馈人")
    public Result removeNotice(@RequestParam("id") Integer id){
        Result r = new Result();
        if (ObjectUtils.isEmpty(id)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR).setMsg("id为必传项");
            return r;
        }
        if(noticeService.removeById(id)){
            r.setCode(ResponseStateConstant.OPERA_SUCCESS).setMsg("删除成功");
        } else {
            r.setCode(ResponseStateConstant.OPERA_FAIL).setMsg("删除失败");
        }
        return r;
    }

    /**
     * 添加或修改反馈人
     * @param notice
     * @param result
     * @return
     */
    @RequestMapping(value = "/updateNotice",method = RequestMethod.POST)
    @ApiOperation("添加或修改反馈人")
    @MarkLog(operate = OperaTypeEnum.UPDATE,tables = TableEnum.NOTICE, desc = "管理员添加或修改反馈人")
    @SensitiveFilter
    public Result insertOrUpdateRole(@RequestBody @Valid NoticeInsertUpdateDto notice, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        noticeService.insertOrUpdateNotice(notice,r);
        return r;
    }
}

